﻿using UnityEngine;
using System.Collections;

public class FileLoader : FileSaver {

	[HideInInspector] public ExoSaveData loadedData;
	GameObject tempUI;
	public SampleInventoryHandler sampleInventoryHandler;
	public BacklogDisplayer backlogDisplayer;
	public bool fromMainMenu;

	private bool fromSavePoint;
	void Awake()
	{
		if(fromMainMenu == false){
			sampleInventoryHandler = GameObject.Find("InventoryManager").GetComponent<SampleInventoryHandler>();
			backlogDisplayer = GameObject.Find("DialogueManager").GetComponent<BacklogDisplayer>();
			fromSavePoint = false;
		}
	}

	//to copy/paste to the other functions
	void LoadFile1(){
		string dataLocation = Application.persistentDataPath + "/SaveFile1";

		if(SerializeUtility.IsFileExist(dataLocation)) //fetch the saved data
		{
			loadedData = (ExoSaveData)SerializeUtility.LoadObject(dataLocation, typeof(ExoSaveData));
			Debug.Log("Data Loaded from " + dataLocation);
		}
		else
			Debug.Log("No Such File detected!");
		Debug.Log (loadedData.DateOfSave);
		Debug.Log (loadedData.TimeOfSave);
		Debug.Log (loadedData.currentChapter);
		Debug.Log (loadedData.InventoryIDs.Count);
		Debug.Log (loadedData.ProfileIDs.Count);
		Debug.Log (loadedData.BackLog);
	}

	public void LoadFile(string sender)
	{
		string extraNum = sender[sender.Length - 1].ToString();
		Debug.Log(extraNum);
		string dataLocation = Application.persistentDataPath + "/SaveFile" + extraNum;
		
		if(SerializeUtility.IsFileExist(dataLocation)) //fetch the saved data
		{
			loadedData = (ExoSaveData)SerializeUtility.LoadObject(dataLocation, typeof(ExoSaveData));
			Debug.Log("Data Loaded from " + dataLocation);
		}
		else
		{
			Debug.Log("No Such File detected!");
			return;
		}

		fromSavePoint = true;
		//1) Date of Save - On Display
		//2) Time of Save - On Display

		//3) Current Level
		Application.LoadLevel(loadedData.applicationLevel);
	}

	public void QuickLoad() {
		string dataLocation = Application.persistentDataPath + "/QuickSaveFile";
		
		if(SerializeUtility.IsFileExist(dataLocation)) //fetch the saved data
		{
			loadedData = (ExoSaveData)SerializeUtility.LoadObject(dataLocation, typeof(ExoSaveData));
			Debug.Log("Data Loaded from " + dataLocation);
		}
		else
		{
			Debug.Log("No Such File detected!");
			return;
		}
		
		fromSavePoint = true;
		
		Application.LoadLevel(loadedData.applicationLevel);
	}

	void OnLevelWasLoaded(int level)
	{
		if(Application.loadedLevel == loadedData.applicationLevel)
			if(fromSavePoint)
			{
				GameObject scm = GameObject.Find("SceneChangeManager");
				GameObject dm = GameObject.Find("DialogueManager");
				
				//4) Current Camera Orientation
				scm.GetComponent<CameraManager>().changeCamera(loadedData.cameraIndex);
				//5) Current Dialogue
				dm.GetComponent<DialogueManager>().dm.setDialogueID((DialoguerDialogues)loadedData.currentChapter);
				//6) Current Inventory IDs
				//7) Current Profile IDs
				//8) Current Profile Descs
				#region Load Inventory and Profiles
				sampleInventoryHandler.LoadFromSaveFile(loadedData.InventoryIDs, loadedData.ProfileIDs, loadedData.ProfileDescs);
				#endregion
				
				//9) Current Triggered Flags
				//10) Current Triggered Floats
				#region Re-assign all flags
				for(int i = 0; i < loadedData.boolFlags.Count; i++)
					Dialoguer.SetGlobalBoolean(i, loadedData.boolFlags[i]);
				for(int i = 0; i < loadedData.floatFlags.Count; i++)
					Dialoguer.SetGlobalFloat(i, loadedData.floatFlags[i]);
				#endregion
				
				//11)Backlog
				#region Load Backlog
				backlogDisplayer.backLogData.allLines = loadedData.BackLog;
				#endregion

				fromSavePoint = false;
			}
	}
}
